package com.anjubao.parking.municipal.parkinglot.controller;

import com.anjubao.parking.municipal.common.dto.ApiCode;
import com.anjubao.parking.municipal.common.dto.ApiResult;
import com.anjubao.parking.municipal.common.utils.MunicipalParkingUtils;
import com.anjubao.parking.municipal.parkinglot.dto.GuidanceScreenDto;
import com.anjubao.parking.municipal.parkinglot.dto.GuidanceScreenReturnCodeDto;
import com.anjubao.parking.municipal.parkinglot.dto.VideoPileDto;
import com.anjubao.parking.municipal.parkinglot.form.*;
import com.anjubao.parking.municipal.parkinglot.model.ParkingLotDeviceGuidanceScreen;
import com.anjubao.parking.municipal.parkinglot.service.ParkingLotDeviceGuidanceScreenService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 诱导屏基本信息API
 * @author zhangyunbo
 *
 */
@RestController
@RequestMapping("/parking-lots-device-guidanceScreens")
@Api(value="ParkingLotDeviceVideoPile模块",tags = {"诱导屏管理接口"})
public class ParkingLotDeviceGuidanceScreenController {
	private static final Logger logger = LoggerFactory.getLogger(ParkingLotDeviceGuidanceScreenController.class);

    @Autowired
    private ParkingLotDeviceGuidanceScreenService parkingLotDeviceGuidanceScreenService;

    /**
     * 新增诱导屏
     *
     * @param form
     * @return
     *
     */
    @PostMapping(value = "")
    @ApiOperation(value = "新增诱导屏",notes = "",code = 2000)
    @ApiImplicitParams({
            @ApiImplicitParam(name="form",required = true,dataType = "GuidanceScreenAddForm",paramType = "body")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败"),
    })
    public ApiResult<GuidanceScreenReturnCodeDto> createGuidanceScreen(@RequestBody @Valid GuidanceScreenAddForm form, BindingResult result) {
        if(result.hasErrors()){
            List<ObjectError> ls=result.getAllErrors();
            return new ApiResult<>(ApiCode.STATUS_INTERNAL_ERROR.getCode(), String.valueOf(ls.get(0)));
        }
        logger.info("新增诱导屏 {} ", form);
        ParkingLotDeviceGuidanceScreen entityCreated = parkingLotDeviceGuidanceScreenService.saveGuidanceScreen(form);
        return ApiResult.success(new GuidanceScreenReturnCodeDto().setGuidanceScreenCode(entityCreated.getCode()));
    }


    @PutMapping("/{guidanceScreenCode}")
    @ApiOperation(value = "修改诱导屏信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="form",required = true,dataType = "GuidanceScreenEditForm",paramType = "body"),
            @ApiImplicitParam(name="guidanceScreenCode",value = "诱导屏code",required = true,dataType = "String",paramType = "path")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败"),
    })
    /**
     * 更新
     *
     * @param parkingLotCode
     * @param form
     * @return
     */
    public ApiResult<GuidanceScreenReturnCodeDto> updateVideoPile(@PathVariable String guidanceScreenCode, @RequestBody @Valid GuidanceScreenEditForm form) {
        logger.info("修改诱导屏信息 {} ", form);
        ParkingLotDeviceGuidanceScreen updated = parkingLotDeviceGuidanceScreenService.updateGuidanceScreen(form);
        return ApiResult.success(new GuidanceScreenReturnCodeDto().setGuidanceScreenCode(updated.getCode()));
    }



    @GetMapping("/{guidanceScreenCode}")
    @ApiOperation(value = "查看单个诱导屏接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name="guidanceScreenCode",value = "诱导屏code",required = true,dataType = "String",paramType = "path")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败"),
    })
    /**
     * 查询
     *
     * @param videoPileCode    视频桩编号
     * @return
     */
    public ApiResult<GuidanceScreenDto> getVideoPile(@PathVariable String guidanceScreenCode) {
        logger.info("获取单个诱导屏 {}", guidanceScreenCode);
        GuidanceScreenDto guidanceScreen = parkingLotDeviceGuidanceScreenService.getGuidanceScreenDto(MunicipalParkingUtils.getIdByCode(guidanceScreenCode));
        return ApiResult.success(guidanceScreen);
    }


    @ApiOperation("删除诱导屏接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "guidanceScreenCode", value = "诱导屏code", required = true, dataType = "string", paramType = "path")
    })
    @DeleteMapping("/{guidanceScreenCode}")
    public ApiResult deleteGuidanceScreen(@PathVariable String guidanceScreenCode) {
        parkingLotDeviceGuidanceScreenService.deleteGuidanceScreen(MunicipalParkingUtils.getIdByCode(guidanceScreenCode));
        return ApiResult.success();
    }

    @GetMapping("")
    @ApiOperation(value = "查询诱导屏信息接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name="isOnline",value = "在线状态 0:断线 1:在线", dataType = "int" ,paramType = "query",allowableValues = "1,0",type = "integer"),
            @ApiImplicitParam(name="status",value = "状态 -1：故障，0：禁用，1：正常", dataType = "int" ,paramType = "query",allowableValues = "1,0,-1",type = "integer")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 5000, message = "操作失败"),
    })
    /**
     * 查询
     *
     * @param form
     * @return
     */
    public ApiResult<List<GuidanceScreenDto>> getGuidanceScreens(GuidanceScreenQueryForm form) {
        logger.info("查询诱导屏信息 {}", form);
        Page<VideoPileDto> page=new Page<>();
        page.setSize(form.getPageSize());
        page.setCurrent(form.getPage());
        Page<GuidanceScreenDto>  guidanceScreens = parkingLotDeviceGuidanceScreenService.getGuidanceScreens(page,form);
        return ApiResult.success(guidanceScreens.getRecords(), (int) guidanceScreens.getTotal());
    }


}
